﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using ThreeByte.ActiveDeck.Config;
using System.Xml.Linq;
using System.Net;
using System.Xml;
using System.ComponentModel;
using ThreeByte.ActiveDeck.Data;
using ThreeByte.ActiveDeck.Service;
using System.Threading;
using ThreeByte.ActiveDeck.Content;
using Microsoft.Office.Interop.PowerPoint;
using ThreeByte.ActiveDeck.AddIn.Config;
using ThreeByte.Media;
using ThreeByte.ActiveDeck.AddIn.Controls;


namespace ThreeByte.ActiveDeck.Controls
{
    /// <summary>
    /// Interaction logic for PageChooserControl.xaml
    /// </summary>
    public partial class PageChooserControl : UserControl, ICloseable, INotifyPropertyChanged
    {

        public event EventHandler<DialogResultEventArgs> Close;
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName) {
            if(PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public static readonly DependencyProperty HOffsetProperty = DependencyProperty.Register("HOffset", typeof(int), typeof(PageChooserControl));
        public static readonly DependencyProperty VOffsetProperty = DependencyProperty.Register("VOffset", typeof(int), typeof(PageChooserControl));

        public int HOffset {
            get {
                return (int)GetValue(HOffsetProperty);
            }
            set {
                SetValue(HOffsetProperty, value);
            }
        }

        public int VOffset {
            get {
                return (int)GetValue(VOffsetProperty);
            }
            set {
                SetValue(VOffsetProperty, value);
            }
        }

        public CoreConfig Config;

        public PageChooserControl(CoreConfig config) {
            InitializeComponent();
            Config = config;
            Tag = "Choose Page"; //Implemented to support customized title bar in Forms Host

            CurrentConfigTreeView.ItemsSource = Config.Slides.OrderBy(p => p.SlideNumber);
            CurrentConfigTreeView.SelectedItemChanged += new RoutedPropertyChangedEventHandler<object>(CurrentConfigTreeView_SelectedItemChanged);

            DoneButton.DataContext = this;
        }

        void CurrentConfigTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e) {
            PageConfig newPage = e.NewValue as PageConfig;
            PreviewRoot.Content = null;

            if(newPage != null) {
                PreviewRoot.Content = PagePreviewFactory.Create(newPage);
            }

            if(newPage is NavigatorConfig) {
                //Bind to the H/V Offsets
                NavigatorConfig navPage = (NavigatorConfig)newPage;
                Binding HBinding = new Binding("OffsetX") { Source = navPage };
                BindingOperations.SetBinding(this, HOffsetProperty, HBinding);
                Binding VBinding = new Binding("OffsetY") { Source = navPage };
                BindingOperations.SetBinding(this, VOffsetProperty, VBinding);
                OffsetDisplay.Visibility = System.Windows.Visibility.Visible;
            } else {
                BindingOperations.ClearBinding(this, HOffsetProperty);
                BindingOperations.ClearBinding(this, VOffsetProperty);
                OffsetDisplay.Visibility = System.Windows.Visibility.Collapsed;
            }

            NotifyPropertyChanged("SelectedPage");
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e) {
            if(Close != null) {
                Close(this, new DialogResultEventArgs(System.Windows.Forms.DialogResult.Cancel));
            }
        }

        public PageConfig SelectedPage {
            get {
                return (CurrentConfigTreeView.SelectedItem) as PageConfig;
            }
        }

        private void DoneButton_Click(object sender, RoutedEventArgs e) {
            if(Close != null) {
                Close(this, new DialogResultEventArgs(System.Windows.Forms.DialogResult.OK));
            }
        }

    }
}
